/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package projecteuler.problems.NameScores22;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.PrintWriter;

/**
 *
 * @author CJ
 */
public class NameScores {

    public static void buildOutputFile(){
        try{
                BufferedReader f = new BufferedReader(
                            new FileReader("C:\\Users\\CJ\\Documents\\NetBeansProjects\\ProjectEuler\\resources\\nameScore22\\names.txt"));
                PrintWriter out = new PrintWriter(
                            new BufferedWriter(
                            new PrintWriter("C:\\Users\\CJ\\Documents\\NetBeansProjects\\ProjectEuler\\resources\\nameScore22\\namesSorted.txt")));

                String[] storage = (f.readLine()).split(",");
                for(int i = 0; i < storage.length; i++){
                    storage[i] = storage[i].substring(1, storage[i].length() - 1);
                }
              /*  for(int i = 0; i < storage.length; i++){
                    System.out.println(storage[i]);
                }*/

                //Bubble ftw

                for(int k = 0; k < storage.length; k++){
                    for(int i = 0; i < storage.length - k - 1; i++){
                        String currentString = storage[i];
                        String comparingString = storage[i + 1];
                        int length = Math.min(currentString.length(), comparingString.length());
                        boolean found = false;
                        for(int j = 0; j < length; j++){
                            int currentStringValue = (int)(currentString.charAt(j));
                            int comparingStringValue = (int)(comparingString.charAt(j));
                            if(currentStringValue > comparingStringValue){
                                storage[i] = comparingString;
                                storage[i + 1] = currentString;
                                found = true;
                                break;
                            }else if(currentStringValue < comparingStringValue){
                                found = true;
                                break;
                            }
                        }
                        if(!found){
                            if(length == comparingString.length()){
                                storage[i] = comparingString;
                                storage[i + 1] = currentString;
                            }
                        }
                    }
                }

                for(int i = 0; i < storage.length; i++){
                    System.out.println(storage[i]);
                    out.println(storage[i]);
                }

                out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void findSum(){
        try{
            BufferedReader f = new BufferedReader(
                            new FileReader("C:\\Users\\CJ\\Documents\\NetBeansProjects\\ProjectEuler\\resources\\nameScore22\\namesSorted.txt"));

            int count = 0;
            int totalWeight = 0;
            String currentName = f.readLine();
            while(currentName != null){
                count++;

                //Calculate weight
                int weight = 0;
                for(int i = 0; i < currentName.length(); i ++){
                    weight += (int)(currentName.charAt(i)) - 64;
                }
                totalWeight += weight * count;
                currentName = f.readLine();
            }
            System.out.println(totalWeight);

        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
